﻿<phone:PhoneApplicationPage xmlns:toolkit="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls.Toolkit" 
    x:Class="FindMyChamp.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:sort="clr-namespace:System.ComponentModel;assembly=System.Windows"
    xmlns:controls="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone.Controls"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:bing="clr-namespace:Microsoft.Phone.Controls.Maps;assembly=Microsoft.Phone.Controls.Maps"
    xmlns:FMCHelpers="clr-namespace:FindMyChamp.Helpers"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="696" 
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="PortraitOrLandscape"  Orientation="Portrait"
    shell:SystemTray.IsVisible="True"
    OrientationChanged="MainPage_OrientationChanged">

    <!-- ApplicationPage Resources -->
    <phone:PhoneApplicationPage.Resources>

        <FMCHelpers:VisibilityConverter x:Key="VisibilityConverter" />
        <FMCHelpers:BooleanConverter x:Key="BooleanConverter" />

        <!-- all champs -->
        <CollectionViewSource x:Name="MyViewSource1" Source="{Binding Champs}" Filter="MyViewSource1_Filter" >
            <CollectionViewSource.SortDescriptions>
                <sort:SortDescription PropertyName="LastName" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

        <!-- local champs -->
        <CollectionViewSource x:Name="MyViewSource2" Source="{Binding Champs}" Filter="MyViewSource2_Filter">
            <CollectionViewSource.SortDescriptions>
                <sort:SortDescription PropertyName="DistanceAway" />
                <sort:SortDescription PropertyName="LastName" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>

        <!-- champs listbox item -->
        <DataTemplate x:Key="ChampListTemplate">
            <Grid Background="Transparent" MinHeight="110">
                <StackPanel Orientation="Horizontal" Width="450">
                    <Image Source="{Binding Image}" Height="100" Width="100" HorizontalAlignment="Left" VerticalAlignment="Top" />
                    <StackPanel Orientation="Vertical" Margin="20,0,0,0" Width="329">
                        <TextBlock Foreground="White" Text="{Binding FullName}" FontSize="29.333" FontWeight="Bold" TextTrimming="WordEllipsis" TextWrapping="Wrap" />
                        <TextBlock Foreground="White" Text="{Binding DistanceAway, StringFormat=distance (km): \{0:#\,##0\}}"
                            Visibility="{Binding DistanceAway, Converter={StaticResource VisibilityConverter}}"/>
                        <TextBlock Foreground="White" Text="{Binding Phone}"/>
                    </StackPanel>
                </StackPanel>
            </Grid>
        </DataTemplate>

    </phone:PhoneApplicationPage.Resources>

    <Grid x:Name="LayoutRoot">
        <Grid.Background>
            <ImageBrush Stretch="Fill" ImageSource="/FindMyChamp;component/ScreenBackgd.png"/>
        </Grid.Background>

        <!-- spinner -->
        <toolkit:PerformanceProgressBar 
            Name="MyProgressBar" IsIndeterminate="True" Visibility="{Binding Path=ProgressBarVisibility, FallbackValue=Collapsed}" VerticalAlignment="Top" />

        <!-- Map Control -->
        <bing:Map x:Name="map" Margin="0,0,0,0" RenderTransformOrigin="0.5,0.5" ZoomLevel="10" Visibility="Collapsed" 
                  CredentialsProvider="ApuRU8XyHtsPkQP7KobGyKT3tQ7JWuQfaoCs7PXMaDNTfwlcgKWhjezUyAtKFYal" 
                  CopyrightVisibility="Collapsed" LogoVisibility="Collapsed" HorizontalAlignment="Center" 
                  ZoomBarVisibility="Visible" AnimationLevel="Full" Width="800" Height="480">

            <!-- pins of champs -->
            <bing:MapLayer>
                <bing:MapItemsControl ItemsSource="{Binding Source={StaticResource MyViewSource1}}">
                    <bing:MapItemsControl.ItemTemplate>
                        <DataTemplate>
                            <bing:Pushpin Location="{Binding Location}" Tap="Pushpin_Tap" Content="{Binding}">
                                <bing:Pushpin.ContentTemplate>
                                    <DataTemplate>
                                        <Grid Background="Transparent" MinHeight="60">
                                            <StackPanel Orientation="Horizontal" Width="200">
                                                <!-- champ pic -->
                                                <Image Source="{Binding Image}" Height="50" Width="50" HorizontalAlignment="Left" VerticalAlignment="Top" />
                                                <StackPanel Orientation="Vertical" Margin="20,0,0,0" Width="130">
                                                    <!-- champ name -->
                                                    <TextBlock Foreground="White" Text="{Binding FullName}" FontSize="16" FontWeight="Bold" TextTrimming="WordEllipsis" TextWrapping="Wrap" />
                                                    <!-- champ is local -->
                                                    <TextBlock Text="{Binding Path=Count, Source={StaticResource MyViewSource2}, StringFormat=1 of \{0\} Local}" Visibility="{Binding IsLocal, Converter={StaticResource BooleanConverter}}"/>
                                                </StackPanel>
                                            </StackPanel>
                                        </Grid>
                                    </DataTemplate>
                                </bing:Pushpin.ContentTemplate>
                            </bing:Pushpin>
                        </DataTemplate>
                    </bing:MapItemsControl.ItemTemplate>
                </bing:MapItemsControl>
            </bing:MapLayer>

            <!-- my location -->
            <bing:Pushpin Location="{Binding CurrentLocation, Mode=TwoWay}" Content="{Binding CurrentLocation, Mode=TwoWay}">
                <bing:Pushpin.Template>
                    <ControlTemplate>
                        <Grid Height="29.284" Width="29.284">
                            <Rectangle Height="20" Width="20" StrokeLineJoin="Miter" RenderTransformOrigin="0.5, 0.5" Fill="Black" Stroke="White" UseLayoutRounding="True" />
                            <Ellipse Height="10" Width="10" Stretch="Fill" StrokeLineJoin="Miter" Stroke="Yellow" Fill="Yellow" />
                        </Grid>
                    </ControlTemplate>
                </bing:Pushpin.Template>
            </bing:Pushpin>

        </bing:Map>

        <!--Pivot Control-->
        <controls:Pivot x:Name="pivot" Title="FIND MY CHAMP">

            <!--Pivot item one-->
            <controls:PivotItem Header="all" Name="all" ScrollViewer.VerticalScrollBarVisibility="Disabled">
                <Grid>
                    <TextBox x:Name="SearchTextBox" TextChanged="SearchTextBox_TextChanged" VerticalAlignment="Top" />
                    <Image HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0,12,10,0" Source="appbar.feature.search.rest.png" Stretch="None"/>
                    <ListBox 
                        x:Name="FirstListBox" 
                        Margin="0,70,0,0" SelectedIndex="-1"
                        ScrollViewer.VerticalScrollBarVisibility="Auto"
                        SelectionChanged="MainListBox_SelectionChanged"
                        ItemsSource="{Binding Source={StaticResource MyViewSource1}}"
                        ItemTemplate="{StaticResource ChampListTemplate}" />
                </Grid>
            </controls:PivotItem>

            <!--Pivot item two-->
            <controls:PivotItem Name="local" xmlns:sort="clr-namespace:System.ComponentModel;assembly=System.Windows">
                <controls:PivotItem.Header>
                    <TextBlock Text="{Binding Path=Count, Source={StaticResource MyViewSource2}, StringFormat=\{0\} local}" />
                </controls:PivotItem.Header>
                <ListBox
                    x:Name="SecondListBox"
                    Margin="0,0,-12,0"
                    SelectionChanged="MainListBox_SelectionChanged"
                    ItemsSource="{Binding Source={StaticResource MyViewSource2}}"
                    ItemTemplate="{StaticResource ChampListTemplate}" />
            </controls:PivotItem>

        </controls:Pivot>

    </Grid>

    <phone:PhoneApplicationPage.ApplicationBar>
        <shell:ApplicationBar IsVisible="True" IsMenuEnabled="True" Mode="Minimized">
            <shell:ApplicationBar.MenuItems>
                <shell:ApplicationBarMenuItem Text="about" x:Name="AboutMenuItem" Click="AboutMenuItem_Click"/>
                <shell:ApplicationBarMenuItem Text="refresh list" x:Name="RefreshMenuItem" Click="RefreshMenuItem_Click"/>
                <shell:ApplicationBarMenuItem Text="privacy information" x:Name="PrivacyMenuItem" Click="PrivacyMenuItem_Click"/>
                <shell:ApplicationBarMenuItem Text="rate this" x:Name="RateThisMenuItem" Click="RateThisMenuItem_Click"/>
                <shell:ApplicationBarMenuItem Text="share this app" x:Name="ShareThisMenuItem" Click="ShareThisMenuItem_Click"/>
            </shell:ApplicationBar.MenuItems>
        </shell:ApplicationBar>
    </phone:PhoneApplicationPage.ApplicationBar>

</phone:PhoneApplicationPage>